# --- Script Explanation ---
# Function: Performs a full, clean rebuild and reinstall of the axiom-view framework.
# How to use: THIS SCRIPT MUST BE LOCATED AND RUN FROM WITHIN THE 'axiom-view' DIRECTORY.

# --- Global Settings ---
# If any command fails, the script will stop immediately.
$ErrorActionPreference = "Stop"

# --- Configuration ---
# The script is running inside the axiom-view directory.
$AxiomViewDir = (Get-Location).Path
# The demo project is located at the specified relative path.
$DemoDir = Join-Path -Path $AxiomViewDir -ChildPath "..\..\Axiom_demo\axiom-view-demo\pc"

# --- Step 0: Path Verification ---
Write-Host "--- Verifying paths before execution ---" -ForegroundColor Yellow
Write-Host "Framework Directory (Current): $AxiomViewDir"
Write-Host "Demo Directory (Calculated):   $DemoDir"

if (-not (Test-Path -Path $DemoDir)) {
    Write-Host "FATAL ERROR: The calculated demo directory does not exist. Please check the path logic in the script." -ForegroundColor Red
    Write-Host "The script tried to find the demo directory at this fully resolved path: $(Resolve-Path -Path $DemoDir -ErrorAction SilentlyContinue)" -ForegroundColor Red
    exit 1
}
Write-Host "Path verification successful. Proceeding with the script." -ForegroundColor Green

# --- Start Execution ---
Write-Host "--- Starting AxiomView Full Clean Rebuild Script ---" -ForegroundColor Green

# Step 1: Deep clean everything
Write-Host "[1/7] Cleaning all artifacts and node_modules..." -ForegroundColor Cyan
Remove-Item -Path (Join-Path -Path $AxiomViewDir -ChildPath "dist") -Recurse -Force -ErrorAction SilentlyContinue
Remove-Item -Path (Join-Path -Path $AxiomViewDir -ChildPath "axiom-view-*.tgz") -Force -ErrorAction SilentlyContinue
Remove-Item -Path (Join-Path -Path $AxiomViewDir -ChildPath "node_modules") -Recurse -Force -ErrorAction SilentlyContinue
Remove-Item -Path (Join-Path -Path $DemoDir -ChildPath "node_modules") -Recurse -Force -ErrorAction SilentlyContinue
Remove-Item -Path (Join-Path -Path $DemoDir -ChildPath "package-lock.json") -Force -ErrorAction SilentlyContinue
Write-Host "Full cleanup complete."

# Step 2: Install axiom-view dependencies
Write-Host "[2/7] Installing axiom-view dependencies..." -ForegroundColor Cyan
Set-Location -Path $AxiomViewDir
npm install
Write-Host "Framework dependencies installed."

# Step 3: Build the framework
Write-Host "[3/7] Building axiom-view..." -ForegroundColor Cyan
npm run build
Write-Host "Build successful."

# Step 4: Pack the framework
Write-Host "[4/7] Packing axiom-view..." -ForegroundColor Cyan
$PackageFileName = (npm pack --json | ConvertFrom-Json)[0].filename
if (-not $PackageFileName) {
    throw "Could not get package filename from npm."
}
Write-Host "Packed successfully: $PackageFileName"
$PackageFullPath = (Resolve-Path -Path $PackageFileName).Path

# Step 5: Install demo dependencies
Write-Host "[5/7] Installing demo dependencies..." -ForegroundColor Cyan
Set-Location -Path $DemoDir
npm install
Write-Host "Demo dependencies installed."

# Step 6: Install local framework package
Write-Host "[6/7] Installing local package '$PackageFullPath'..." -ForegroundColor Cyan
npm install $PackageFullPath --save-dev
Write-Host "Local package installed."

# --- Finish ---
Set-Location -Path $AxiomViewDir
Write-Host "[7/7] --- Script finished successfully! ---" -ForegroundColor Green 